package com.lagou.client;

import com.lagou.service.UserService;
import io.netty.channel.ChannelFuture;

import java.nio.channels.ClosedChannelException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class ClientBootStrap {

    public static void main(String[] args) throws InterruptedException {

        ZkServerGeter.getZkServer();

        ConcurrentHashMap<String,String> map = ZkServerGeter.getPaths();

        //遍历map集合
        while (true){


        for(Map.Entry<String, String> a:map.entrySet()){


                //    System.out.println("键是"+a.getKey()+"值是"+a.getValue());

                String[] ipHost = a.getValue().split(":");

                int port = Integer.parseInt(ipHost[1]);
                int heartPort = Integer.parseInt(ipHost[2]);


                if (map.containsKey(a.getKey())){

                    System.out.println("开始调用服务器"+a.getKey()+a.getValue());
//                    Thread.sleep(2000);

                    if (NettyClient.heartBeatTest(ipHost[0],heartPort)) {
                        RpcConsumer rpcConsumer = new RpcConsumer();
                        UserService proxy = (UserService) rpcConsumer.createProxy(UserService.class,ipHost[0],port);
                        Object o = proxy.sayHello("are you ok?");
                        System.out.println("代理执行结果："+o.toString());
                    }else {
                        System.out.println("服务器已下线");
                    }


                }
                System.out.println("服务器"+a.getKey()+"调用结束");
                Thread.sleep(2000);


            }

            System.out.println("所有服务器都调用过一遍了");
            Thread.sleep(2000);

        }













    }




}
